package itsm.isperp.module.controller.task;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.task.TskInspectCiInfo;
import itsm.isperp.module.service.configuration.ConfCiService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.task.TskInspectCiInfoService;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2015-05-11 11:51
 */
@Controller
@RequestMapping("task/inspectCiInfo")
public class TskInspectCiInfoController extends
		BaseController<TskInspectCiInfo> {

	@Autowired
	protected TskInspectCiInfoService tskInspectCiInfoService;

	@Autowired
	protected ConfCiService confCiService;

	@Override
	public TskInspectCiInfoService getService() {
		return this.tskInspectCiInfoService;
	}

	@Override
	public String getPageTitle() {
		return "巡检资产";
	}

	@RequestMapping("saveWithCi")
	@ResponseBody
	public ResultMessage saveWithCi(String ciId, String relationId,
			HttpServletRequest req) {
		if (StringUtils.isEmpty(relationId)) {
			return ResultMessage.failure("关联id不能为空！");
		}
		if (ciId.indexOf(",") > -1) {

			String[] ciIds = ciId.split(",");
			for (String s : ciIds) {
				TskInspectCiInfo model = new TskInspectCiInfo();
				model.setCiId(s);
				model.setTaskId(relationId);
				this.getService().saveOrUpdate(model);

			}
			return ResultMessage.success();
		} else {
			TskInspectCiInfo model = new TskInspectCiInfo();
			model.setCiId(ciId);
			model.setTaskId(relationId);
			this.getService().saveOrUpdate(model);
			return ResultMessage.success();
		}

	}

	@RequestMapping("listCi")
	@ResponseBody
	public PageResponse<Map<String, Object>> listCi(JqGridRequest request,
			HttpServletRequest req) {
		String relationId = req.getParameter("relationId");

		String projectId = req.getParameter("projectId");

		ProjProjectService projProjectService = ContextHolder
				.getSpringBean("projProjectService");
		Map<String, Object> map = projProjectService.findOneToMap(projectId,
				"cus_customer_id");

		request.addFilterRule("cus_customer_id", JqGridFilterRuleOp.eq, map
				.get("cus_customer_id").toString());

		request.setSearchString(" a.id NOT IN (SELECT ci_id FROM tsk_inspect_ci_info WHERE task_id='"
				+ relationId + "')");

		return this.confCiService.findAllDetailByRequest(request).toPage();

	}

	@RequestMapping("listDetail")
	@ResponseBody
	@Override
	public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
			HttpServletRequest req) {

		String relationId = req.getParameter("relationId");
		if (StringUtils.isNotEmpty(relationId)) {
			request.addFilterRule("task_id", JqGridFilterRuleOp.eq, relationId);
		}

		return this.getService().findAllDetailByRequest(request).toPage();

	}

	@RequestMapping("auditEdit")
	public ModelAndView auditEdit(String id, HttpServletRequest req) {
		ModelAndView mv = new ModelAndView(this.getModulePath() + "/auditEdit");
		String ids = req.getParameter("ids");
		mv.addObject("ids", ids);

		return mv;

	}

	@RequestMapping("saveAudit")
	@ResponseBody
	public ResultMessage saveAudit(HttpServletRequest req) {

		String ids = req.getParameter("ids");
		String auditResult = req.getParameter("auditResult");
		String auditDate = req.getParameter("auditDate");

		// confCiService.saveAuditCi(ids, auditResult, auditDate);
		tskInspectCiInfoService.saveAuditCi(ids, auditResult, auditDate);
		return ResultMessage.success("");
	}

}
